<?php

//Load QCubed Framework...
require_once('../../../qcubed.inc.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');

class Login extends QForm {

    protected $objAlert, $objWaitIcon, $objToogleEnableAction;
    protected $txtUsername, $txtPassword, $chkRemeberMe;
    protected $btnLogin;
    
    protected function Form_Create() {
        parent::Form_Create();

        QApplication::ExecuteJavaScript('Login.init();');
        
        $this->objAlert = new Alert($this);
        
        $this->objDefaultWaitIcon = $this->objWaitIcon = new QWaitIcon($this);
        $this->objWaitIcon->Text = '<i class="icon-cog icon-spin icon-large"></i>';

        $this->txtUsername = new QTextBox($this);
        $this->txtUsername->Name = QApplication::Translate('Username');
        $this->txtUsername->LabelForRequiredUnnamed = QApplication::Translate('Username is required');

        $this->txtPassword = new QTextBox($this);
        $this->txtPassword->Name = QApplication::Translate('Password');
        $this->txtPassword->TextMode = QTextMode::Password;
        $this->txtPassword->LabelForRequiredUnnamed = QApplication::Translate('Password is required');

        $this->txtUsername->Required =
        $this->txtPassword->Required = true;

        $this->chkRemeberMe = new QCheckBox($this);
        $this->chkRemeberMe->Name = QApplication::Translate('Remember Me');
        $this->chkRemeberMe->SetCustomAttribute('data-label', $this->chkRemeberMe->Name);
        
        $this->btnLogin = new QButton($this);
        $this->btnLogin->HtmlEntities = false;
        $this->btnLogin->Text = QApplication::Translate('Sign In');
        $this->btnLogin->PrimaryButton =
        $this->btnLogin->CausesValidation = true;
        
        $this->objToogleEnableAction = new QToggleEnableAction($this->btnLogin);
        
        $this->btnLogin->AddAction(new QClickEvent(), $this->objToogleEnableAction);
        $this->btnLogin->AddAction(new QClickEvent(), new QAjaxAction('btnLogin_Click'));
        $this->btnLogin->AddAction(new QClickEvent(), new QTerminateAction());
    }
    
    protected function Form_Validate() {
        $blnToReturn = parent::Form_Validate();
        
        $arrErrors = $this->GetErrorControls();
        
        if (!empty($arrErrors))
            QApplication::ExecuteJavascript($this->objToogleEnableAction->RenderScript($this->btnLogin));
        
        return $blnToReturn;
    }

    protected function btnLogin_Click($strFormId, $strControlId, $strParameter) {
        try {
                $objUser = User::LoadByUsername($this->txtUsername->Text);

                if (!$objUser || $objUser->Password != $this->txtPassword->Text) {
                    
                    $this->txtPassword->Text = '';
                    $this->objAlert->Error('Ooops!', 'Unknown user or password');
                
                } else {

                    $_SESSION['signed_in'] = true;
                    $_SESSION['user'] = serialize($objUser);

                    QApplication::Redirect('../dashboard');
                }
                
        } catch (QCallerException $objExc) {
            
            $objExc->IncrementOffset();
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }
        
        QApplication::ExecuteJavascript($this->objToogleEnableAction->RenderScript($this->btnLogin));
    }
}

Login::Run('Login', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/membership/login.tpl.php');
?>